const router = require('express').Router()
const sql = require('./../sql')
const Pircture = require('./../sql/models/Pircture')
const fs = require("fs");
const multiparty = require('multiparty');
const images = require("images");
const utils = require('./../utils')
// var formidable = require('./node_modules/formidable');
// 上传图片
/**
 * @api {post} /api/picture/uploadImage 上传图片
 * @apiName uploadImage
 * @apiGroup picture
 * 
 * @apiSuccess {String} code 状态码
 * @apiSuccess {String} message 描述
 * 
 * @apiSuccessExample Success-Response：
 *      HTTP/1.1 200 OK
 *      {
 *        "code":"200",
 *        "message":"上传成功",
 *          data 
 *      }
 * @apiSampleRequest /api/picture/uploadImage
 */
router.post('/uploadImage', function(req, res) {
    var form = new multiparty.Form();
    form.parse(req, function(err, fields, files) {
        images(files.file[0].path) //Load image from file
            //加载图像文件
            .size(1920) //Geometric scaling the image to 400 pixels width
            //等比缩放图像到400像素宽
            //在(10,10)处绘制Logo
            .save("public/images/" + "filebigName.png", { //Save the image to a file,whih quality 50
                quality: 80 //保存图片到文件,图片质量为50
            });
            console.log('files',files)
            // const insertData = files.file
            let insertData = {}
            insertData = {
                picid: "images_"+ utils.getUuid(),
                picurl:files.file[0].originalFilename,
                createTime : new Date(),
                size:files.file[0].size
            }
            sql.insert(Pircture,insertData).then(()=>{
                res.status(200).send({
                    code:"200",
                    message:"上传成功",
                    data:files
                })
            })    
    });
});

router.get('/getUploadImage',function(req,res){
    sql.find(Pircture).then(()=>{
        console.log(res,'数据');
        res.status(200).send({
            code:"200",
            message:"获取成功",
            data:res.data
        })
    })
})
module.exports = router